home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17180 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  12.2 KB

  1. Path: newsfeed.concentric.net!news
  2. From: "Alan L. Lovejoy" <alovejoy@concentric.net>
  3. Newsgroups: comp.lang.java,comp.lang.c++,comp.lang.smalltalk
  4. Subject: Re: Will Java kill C++?
  5. Date: Sun, 14 Apr 1996 01:04:16 -0700
  6. Organization: Modulation
  7. Message-ID: <3170B180.51A8@concentric.net>
  8. References: <31682FFE.2781E494@bbn.com> <DpJyGG.FKK@hkuxb.hku.hk> <denatale-1004960822260001@grail1506.nando.net> <dbell-1104960125190001@wholder2.cts.com> <goochb.327.000893D1@rwi.com> <dbell-1104961159050001@wholder2.cts.com> <316D8523.74D7@concentric.net> <dbell-1104962256020001@wholder2.cts.com> <316E3FB6.38F7@concentric.net> <dbell-1204961508460001@wholder2.cts.com> <316F8A35.5189@concentric.net> <dbell-1304961718060001@wholder2.cts.com>
  9. NNTP-Posting-Host: cnc009054.concentric.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01Gold (Win95; U)
  14.  
  15. Doug Bell wrote:
  16. (with parts where substantial agreement has been reached left out)
  17. > "Alan L. Lovejoy" <alovejoy@concentric.net> wrote:
  18. > > Firstly, if Java is to be used to code applets on WWW pages, then it will
  19. > > always be interpreted (when used for that purpose)--unless the world happens
  20. > > to standardize on exactly one instruction set to the exclusion of all others.
  21. > Here's my opportunity to teach you something.  :)
  22. > Currently, Java is executed through interpretation.  This is already
  23. > starting to change as the first "just in time" compilers (or JIT
  24. > compilers) for java are becoming available.  A JIT compiler will translate
  25. > the machine-independent byte-codes of Java into directly executable
  26. > machine code.  This translation happens as the code is loaded off the
  27. > network, and in fact should occur faster than the code is downloaded
  28. > across the network, so there should be *no* additional delays imposed by
  29. > this translation.  The resulting machine code is then executed.  Web
  30. > browsers most certainly will use JIT compilers in the near future.  Stand
  31. > alone Java applications can either be translated as they are loaded, or
  32. > translated once for a platform.
  33. > The performance of this translated code is yet to be determined, but it
  34. > should be able to achieve somewhere between 50% and 100% the speed of
  35. > natively compiled C or C++ code.
  36. > In addition, Sun is already working on processors which will execute the
  37. > Java bytecodes directly.  How these will affect the market remains
  38. > unclear, but it serves to point out that there are even greater
  39. > performance possibilities already in the works.
  40.  
  41. For the past ten years, the ParcPlace (now ParcPlace-Digitalk) VM has worked
  42. more or less as you describe the JIT compiler--although there are differences.
  43. This is why the recently reported benchmarks show Smalltalk as being many
  44. times faster than Java.  I fully expect this to change over the next year.
  45.  
  46. Xerox PARC developed not just Smalltalk, but Smalltalk workstations that
  47. used bit-sliced microcoded processors to execute Smalltalk bytecodes in
  48. hardware.  There was a university project in the 80's that developed a
  49. Smalltalk CPU called SOAR.
  50.  
  51. But these specialized processors suffered the same fate as the infamous
  52. "LISP machines." The commerical RISC processors ate their lunch, for several 
  53. reasons: 
  54.  
  55. 1. The general purpose RISCs would run all software, not just Smalltalk
  56. or LISP.
  57.  
  58. 2. They were often faster than the special purpose processors, because
  59. more resources were expended in their design and manufacture--because
  60. the return on investment was higher.
  61.  
  62. 3. The world wanted to standardize on MS-DOS and UNIX.
  63.  
  64. Another point to consider: Smalltalk (and LISP) would probably benefit from 
  65. a Java processor as much as would Java itself.  At the "bytecode" level,
  66. syntax differences and static typing are no longer relevant.
  67.  
  68. > > The main differences between Java and Smalltalk (as languages, not as
  69. > development
  70. > > environments or delivery platforms):
  71. > >
  72. > > 1. Java is statically typed, Smalltalk is not (although both use dynamic
  73. > binding).
  74. > Maybe this my lack of understanding (as I bow to the Gods of OOP who have
  75. > traveled the road farther than I), but I view Java's strongly typed
  76. > behavior as a benefit.  I realize this is probably different from what you
  77. > mean by statically typed.
  78.  
  79. You have been blindsided by a common misuse of technical terminology.
  80.  
  81. The terms "static typing" and "strong typing" are often wrongfully used as
  82. though they were synonyms.  This misuse is endemic, and many are misled by
  83. it.
  84.  
  85. The correct definition of "static typing" is that a) type constraints are
  86. enforced during program translation ("compilation"); and b) the type of
  87. of an object is always determinable by lexical analysis of the program text.
  88.  
  89. The correct definition of "strong typing" is that type errors are prevented
  90. from occuring.  An example of a type error would be applying the integer
  91. addition machine instruction to a 32-bit floating point value, and then
  92. using the result as though it meant something.
  93.  
  94. Smalltalk, LISP and Java are all strongly typed.  Only Java is also statically
  95. typed. Type errors can occur in both C and C++, so they are statically
  96. but not strongly typed.
  97.  
  98. Whether static typing is good or not is another religious issue that
  99. I will refrain from addressing.
  100.  
  101. > > 2. Smalltalk has block closures.  Java doesn't even have function pointers.
  102. > I keep seeing this.  What is a block closure?
  103.  
  104. Glad you asked!
  105.  
  106. First, let me introduce Smalltalk-like "block closures" into C.
  107.  
  108. C already has "blocks."  They're the things bracketed by "{" and "}".
  109. The syntax is roughly: Block ::= "{" Statement {";" Statement} "}".
  110. For example: while (index <= 10) {printf("%i", index); index = index + 1}.
  111.  
  112. To convert these into block closures, we first have to make them values
  113. that can be assigned to variables and passed as parameters. So we have
  114. to introduce the datatype "block."  Then we can write code like this:
  115.  
  116.     block countingBlock;
  117.     int index;
  118.  
  119.     countingBlock = {printf("%i", index); index = index + 1};
  120.     index = 1;
  121.     while (index <= 10) countingBlock;
  122.  
  123. Next, we have to be able to pass parameters into a block--just like it
  124. was a function.  In fact, you can think of a Smalltalk block as an
  125. anonymous function. Such a parameterized block in C might look like
  126. this: {(int index): printf("%i", index)}.  Then we can write code
  127. like this:
  128.  
  129.     block printBlock;
  130.     int i;
  131.     
  132.     printBlock = {(int index): printf("%i", index)};
  133.     for (i = 1; i <= 10; i++) printBlock(i);
  134.  
  135. Note that "printBlock" could just as easily have been passed in as a parameter
  136. to the function that will execute it instead of being declared just before use.
  137.  
  138. Finally, we have to give the blocks closure semantics. This defines the
  139. meaning of external references (reads and writes to variables declared
  140. outside the scope of the block itself) in a precise way.  Specifically,
  141. the resolution of variable references external to the block has to be
  142. closed at the time the block definition code is executed.  This requires
  143. that function contexts be allocated on the heap and not on the stack,
  144. because a variable that is referred to by a closure is "live" as long as
  145. the closure is.  Another way to think of this is that variables declared
  146. outside the closure, but referenced by the closure, are in effect implied
  147. reference parameters.
  148.  
  149. One of the most powerful uses of blockClosures is for iterating over the
  150. contents of a collection. Let me use my extended C again to illustrate:
  151.  
  152. Suppose we have defined a binary tree data structure, whose nodes are
  153. structs.  There is a typedef for this struct named BinaryTreeNode,
  154. and a set of functions that implement the usual operations on binary
  155. trees. Additionaly, we have defined three tree enumeration functions:
  156. inOrderDo(), inPreOrderDo(), and inPostOrderDo().  All three functions
  157. take a one-argument "block" as an argument:
  158.  
  159.     typedef 
  160.         
  161.         BinaryTreeNode struct {void *left, *right, *value};
  162.         BinaryTreeBlock block {BinaryTreeNode * node};
  163.  
  164.     void doInOrder(BinaryTreeNode node; BinaryTreeBlock enumerationBlock) {...};
  165.     void doInPreOrder(BinaryTreeNode node; BinaryTreeBlock enumerationBlock) {...};
  166.     void doInPostOrder(BinaryTreeNode node; BinaryTreeBlock enumerationBlock) {...};
  167.  
  168. One can then iterate over all nodes in a binary tree, applying whatever logic 
  169. to each node that can be coded by a block closure:
  170.  
  171.     doInOrder(
  172.         tree, 
  173.         {(BinaryTreeNode * node): 
  174.             addBTReeNode(copyOfTree, bTreeNodeCopy(node))}
  175.          );
  176.  
  177. Block closures are also useful for creating new control structures.  In Smalltalk,
  178. all the traditional control structures are implemented using block closures.
  179.  
  180. Of course, Smalltalk block closures aren't just "values."  They're objects.
  181. You can send them messages.  They have a class.  You can add methods to that
  182. class.  
  183.  
  184. > > 3. Smalltalk is highly reflexive and permits metaprogramming.  Java isn't and
  185. > > doesn't (compared to Smalltalk).
  186. > English please... (sorry)
  187.  
  188. A real explanation would take longer than I have.  The IRS wants some of my time
  189. right about now (sorry).  But here are some key points:
  190.  
  191. 1) In Smalltalk, all values are objects.
  192. 2) Classes are values--and hence they are objects. Just like any other object, a class 
  193. is itself an instance of a class.  A class whose instances are themselves classes is 
  194. called a metaclass.  A metaclass is an object, and just like any other object, it is 
  195. an instance of a class.  Specifically, a metaclass is an instance of the class
  196. Metaclass.  Metaclass is an object, and just like any other object, it is an
  197. instance of a class--and that class is an instance of Metaclass, so the relationship
  198. is circular.  Both Metaclass and its class are instances of their own instances.
  199. 3) Classes can be assigned to variables, passed as parameters and sent messages,
  200. just like any other object.
  201. 4) You can ask any object for its class.  This is an example of reflection.
  202. 5) You can add methods to any class, including system classes (for example, the 
  203. classes Object, CompiledMethod, BlockClosure, Class and Metaclass--doing this is
  204. an example of metaprogramming).
  205. 6) You can subclass, reparent or redefine any class--including the system classes.
  206. 7) You can make the system use different classes than those specified by the system
  207. vendor for most system functions (there are some exceptions, and it would be easy
  208. for the system vendor to prevent the substitution of certain classes if that was
  209. required for system security).  For example, you could make the system use some
  210. class other than Metaclass to instantiate metaclasses, or some class other than
  211. CompiledMethod to instantiate methods.  More metaprogramming.
  212. 8) You can create/modify/remove classes programmatically as the program is running.
  213. Yet more metaprogramming.
  214. 9) You can programmatically create annonymous/lightweight classes for special purposes.
  215. Even more powerful metaprogramming.
  216. 10) The execution context is a value--and hence an object.  This object represents
  217. the state of a thread of execution: the method activation stack, the program counter,
  218. the temporary variables and method arguments.  Each thread (called a process in
  219. Smalltalk) stores an object that represents the current state of thread execution
  220. for that thread.  And there is a pseudo-variable 'thisContext' that refers to
  221. the execution state of the current thread.  An execution context can answer questions
  222. such as "what is the CompiledMethod that is currently executing," or "what object is 
  223. the receiver of the message that was sent to invoke this method," or "what is the value 
  224. of the third method argument" or "what context sent the message that invoked this method."
  225. This is deep reflection.
  226. 11) You can ask a class what instance variables its instances have, what messages
  227. its instances will understand and what code will actually execute when messages are sent
  228. to the instances--among other things.  And you can ask methods what messages are sent
  229. in the body of the method, what instance variables are accessed (read or written),
  230. and what global values are accessed.  This, too, is deep reflection.
  231.  
  232. I could go on, but hopefully this begins to give you an idea of the capabilities.
  233.  
  234. These metaprogramming and reflective capabilities can be used to do things such as sending 
  235. objects over the net when the receiver of the objects has never seen or heard of the classes 
  236. of the objects (and you can't send source code because the receiver does not have a compiler),
  237. or sharing of distrubuted objects by processes on different computers connected over the net.
  238.  
  239. --Alan
  240.